<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateStaffOrgAPIRequest;
use App\Http\Requests\API\UpdateStaffOrgAPIRequest;
use App\Models\StaffOrg;
use App\Models\StaffInfo;
use App\Repositories\StaffOrgRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class StaffOrgController
 * @package App\Http\Controllers\API
 */

class StaffOrgAPIController extends AppBaseController
{
    /** @var  StaffOrgRepository */
    private $staffOrgRepository;

    public function __construct(StaffOrgRepository $staffOrgRepo)
    {
        $this->staffOrgRepository = $staffOrgRepo;
    }

    /**
     * Display a listing of the StaffOrg.
     * GET|HEAD /staffOrgs
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->staffOrgRepository->pushCriteria(new RequestCriteria($request));
        $this->staffOrgRepository->pushCriteria(new LimitOffsetCriteria($request));
        $staffOrgs = $this->staffOrgRepository->all()->toArray();
        
        // $data = [];
        $data = makeStaffOrgTree($staffOrgs, 'id', 'parent_id', 'children', 0);

        // $staffOrgs = $this->staffOrgRepository->paginate($request->limit);

        return $this->sendResponse($data, 'Staff Orgs retrieved successfully');
    }

    /**
     * Store a newly created StaffOrg in storage.
     * POST /staffOrgs
     *
     * @param CreateStaffOrgAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateStaffOrgAPIRequest $request)
    {
        $input = $request->all();

        $staffOrgs = $this->staffOrgRepository->create($input);

        return $this->sendResponse($staffOrgs->toArray(), 'Staff Org saved successfully');
    }

    /**
     * Display the specified StaffOrg.
     * GET|HEAD /staffOrgs/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var StaffOrg $staffOrg */
        $staffOrg = $this->staffOrgRepository->findWithoutFail($id);

        if (empty($staffOrg)) {
            return $this->sendError('Staff Org not found');
        }

        return $this->sendResponse($staffOrg->toArray(), 'Staff Org retrieved successfully');
    }

    /**
     * Update the specified StaffOrg in storage.
     * PUT/PATCH /staffOrgs/{id}
     *
     * @param  int $id
     * @param UpdateStaffOrgAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateStaffOrgAPIRequest $request)
    {
        $input = $request->all();

        /** @var StaffOrg $staffOrg */
        $staffOrg = $this->staffOrgRepository->findWithoutFail($id);

        if (empty($staffOrg)) {
            return $this->sendError('Staff Org not found');
        }

        $staffOrg = $this->staffOrgRepository->update($input, $id);

        return $this->sendResponse($staffOrg->toArray(), 'StaffOrg updated successfully');
    }

    /**
     * Remove the specified StaffOrg from storage.
     * DELETE /staffOrgs/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var StaffOrg $staffOrg */
        $staffOrg = $this->staffOrgRepository->findWithoutFail($id);

        if (empty($staffOrg)) {
            return $this->sendError('Staff Org not found');
        }

        if (StaffOrg::where('parent_id', $id)->count() > 0) {
            return $this->sendError('该组织有下属组织，不能直接删除。');
        }

        $count = StaffInfo::where('oid', $id)->count();
        if ($count > 0) {
            return $this->sendError('该组织下已有员工，请先删除员工。');
        }

        $staffOrg->delete();

        return $this->sendResponse($id, 'Staff Org deleted successfully');
    }


    /**
     * Update the specified StaffOrg in storage.
     * PUT/PATCH /staffOrgs/{id}
     *
     * @param  int $id
     * @param UpdateStaffOrgAPIRequest $request
     *
     * @return Response
     */
    public function batch(Request $request)
    {

        $ids  = $request->input('ids');
        $data = [
            'fee_annual'           => floatval($request->input('fee_annual')),
            'fee_annual_oversize'  => floatval($request->input('fee_annual_oversize')),
            'fee_monthly'          => floatval($request->input('fee_monthly')),
            'fee_monthly_oversize' => floatval($request->input('fee_monthly_oversize')),
        ];

        if (empty($ids) || !is_array($ids)) {
            return $this->sendError('ID数组不能为空！');
        }


        $rs = [];
        foreach ($ids as $id) {
            // update
            if ($this->staffOrgRepository->update($data, $id)) {
                $rs[] = $id;
            }
        }

        return $this->sendResponse($rs, 'StaffOrg updated successfully');
    }
}
